package com.portal.framework.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;
import com.portal.framework.exception.AppException;
import com.portal.framework.exception.ExceptionHandleException;

public class ExceptionInterceptorWebwork implements Interceptor {

	private static final long serialVersionUID = 0L;

	public static final String EXCEPTION = "exception";

	public static final String ERROR_PAGE = "fail";// 错误信息页面

//	private WritesMessageLogIBPO writesMessageLog;// 操作日志
//
//	public void setWritesMessageLog(WritesMessageLogIBPO writesMessageLog) {
//		this.writesMessageLog = writesMessageLog;
//	}

	public String intercept(ActionInvocation invocation) throws Exception {
		String result = null;
		try {
			before(invocation);
		} catch (Exception e) {
			printError(invocation, e);
			return ERROR_PAGE;
		}
		try {
			result = invocation.invoke();
		} catch (Exception e) {
			printError(invocation, e);
			return ERROR_PAGE;
		}
		try {
			after(invocation, result);
		} catch (Exception e) {
			printError(invocation, e);
			return ERROR_PAGE;
		}
		return result;
	}

	/**
	 * 在invocation执行完毕之后被调用
	 * 
	 * @param invocation
	 * @param result
	 * @throws Exception
	 */
	private void after(ActionInvocation invocation, String result)
			throws AppException {
	}

	/**
	 * 在invocation执行之前被调用,
	 * 
	 * @param invocation
	 * @throws Exception
	 */
	private void before(ActionInvocation invocation) throws AppException {
		invocation.getStack().setValue("actionName",invocation.getProxy().getActionName());
	}

	public void destroy() {
	}

	public void init() {

	}

	/**
	 * 输出异常并写日志
	 * 
	 * @param invocation
	 * @param e
	 */
	private void printError(ActionInvocation invocation, Exception e)throws AppException {
		//
		e.printStackTrace();
		StringBuffer errormsg = new StringBuffer();
		Throwable cause = e.getCause();
		if (cause != null) {
			errormsg.append(cause.getMessage()+"\n ");
		} else {
			errormsg.append(e.getMessage()+"\n ");
		}
		if(errormsg==null){
			errormsg.append("对象没有实例化 \n ");
		}
		ValueStack stack = invocation.getStack();//.getStack();
		Map<String, Object> vo = new HashMap<String, Object>();
		HttpServletRequest request=ServletActionContext.getRequest();
//		writesMessageLog.createOperationLog(setMessagelogInfo(request,invocation,errormsg.toString()));//写操作日志		System.out.println("Error:(日志系统待优化)");
		vo.put("errormsg", errormsg);
		/**
		 * 判断是否为异常处理拦截器throw的异常,如果是则把stackTraceInfo放到stack中
		 */
		if(e instanceof ExceptionHandleException){
			ExceptionHandleException hanldeException = ((ExceptionHandleException)e);
			vo.put("stackTraceInfo", hanldeException.getStackTraceInfo());
		}
		stack.push(vo);
	}
	
	/**
	 * 填充日志信息
	 * 
	 * @param msgLog
	 */
//	private MessagelogDTO setMessagelogInfo(HttpServletRequest request,
//			ActionInvocation invocation,String errormsg) {
//		MessagelogDTO msgLog = new MessagelogDTO();
//		msgLog.setDatetime(new Date(System.currentTimeMillis()));// 操作时间
//		msgLog.setEnterprisename("测试单位");
//		msgLog.setOrganname("测试机构");
//		msgLog.setUsername("测试用户名");
//		msgLog.setActionname(invocation.getProxy().getActionName());
//		msgLog.setModulename(invocation.getProxy().getConfig().getClassName());
//		msgLog.setRemoteip(request.getRemoteAddr());
//		msgLog.setLevelno(ConstSort.LevelNo_ERROR);
//		msgLog.setMessagecontent(errormsg.length()<=254?errormsg:errormsg.substring(0,254));
//		return msgLog;
//	}

}
